from BeautifulSoup import BeautifulSoup

class Parser(object):

    def __init__(self, api, browser, url):
        self.api = api
        self.browser = browser
        self.url = url

    def fetch_response(self):
        url = self.build_url()
        print "parsing %s" % url

        response = self.browser.open(url)
        #TODO add checking if still logged in and other errors
        return response

    def parse_html(self, html_items, file):
        items = {}
        for html_item in html_items:
            name, url = self.parse_item(html_item)

            item = file(self.api, name, url)
            items[item.name] = item

        return items

    def parse_item(self, html):
        name = self.parse_item_name(html)
        url  = self.parse_item_url(html)

        return name, url

    def parse_item_name(self, html):
        pass

    def parse_item_url(self, html):
        pass

    def get_parsed_data(self):
        response = self.fetch_response()
        soup = BeautifulSoup(response.read())
        return soup
        
    def build_url(self):
        return self.url